package com.demo.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.demo.domain.TeamComposition;
import com.demo.domain.TeamProfile;
import org.apache.ibatis.annotations.*;

import java.util.Date;
import java.util.List;

@Mapper
public interface TeamProfileDao extends BaseMapper<TeamProfile> {
    //    增加对应id的队伍的成员数
    @Update("update tb_team_profile tp set tp.tp_member_number=tp.tp_member_number-1 where tp.tp_id=#{id}")
    int decrementTeamMember(Long id);

    //    减少对应id的队伍的成员数
    @Update("update tb_team_profile tp set tp.tp_member_number=tp.tp_member_number+1 where tp.tp_id=#{id}")
    int incrementTeamMember(Long id);

    @Select("SELECT tp_lack_student_number FROM tb_team_profile WHERE tp_id=#{id}")
    int getLackNumber(Long id);

    //    得到uid未参加的队伍
    @Select("SELECT *\n" +
            "FROM tb_team_profile\n" +
            "WHERE tp_id IN\n" +
            "(SELECT DISTINCT tc_team_id  \n" +
            "FROM tb_team_composition  \n" +
            "WHERE tc_team_id NOT IN (  \n" +
            "    SELECT tc_team_id  \n" +
            "    FROM tb_team_composition  \n" +
            "    WHERE tc_uid = #{uid} \n" +
            ")\n" +
            ")")
    List<TeamProfile> selectNotInTeam(long uid);

    //    得到uid未参加的队伍
    @Select("""
            SELECT tp_id,tp_uid,tp_urealname,tp_team_name,tp_project_name,tp_recruit_total_number,tp_member_number,tp_teacher_number,tp_lack_student_number,tp_registration_deadline,tp_contact,tp_team_intro,tp_ability_need,tp_alive
            FROM tb_team_profile
            WHERE tp_id IN
            (SELECT DISTINCT tc_team_id \s
            FROM tb_team_composition \s
            WHERE tc_uid = #{uid}
            )
            """)
    @Results({@Result(column = "tp_lack_student_number", property = "tpDifference"),
            @Result(column = "tp_recruit_total_number", property = "tpNeedStudentNumber")})
    List<TeamProfile> selectInTeam(Long uid);


    @Update("UPDATE tb_team_profile\n" +
            "set tp_alive=0\n" +
            "WHERE tp_id=#{tpId}")
    int updateTeamNotAlive(Long tpId);

    @Update("UPDATE tb_team_profile tp set tp.tp_del_flag = 1 where tp.tp_id = #{tpId}")
    int deleteTeamProfileById(TeamProfile teamProfile);

    @Select("SELECT * FROM tb_team_profile tp where tp.tp_del_flag = 0")
    List<TeamProfile> listAllTeamProfile();


    @Select("<script>" +
            "SELECT * FROM tb_team_profile tp " +
            "where tp.tp_del_flag = 0" +
            "<if test='uid != null and uid != \"\"'>AND tp.tp_uid = #{uid}</if>" +
            "<if test='urealname != null and urealname != \"\"'>AND tp.tp_urealname = #{urealname}</if>" +
            "<if test='teamName != null and teamName != \"\"'>AND tp.tp_team_name = #{teamName}</if>" +
            "<if test='projectName != null and projectName != \"\"'>AND tp.tp_project_name = #{projectName}</if>" +
            "<if test='registrationDeadline != null and registrationDeadline != \"\"'>AND tp.tp_registration_deadline = #{registrationDeadline}</if>" +
            " ORDER BY tp.tp_id ASC" +
            "</script>")
    List<TeamProfile> selectTeamProfileList(Long uid, String urealname, String teamName,String projectName, Date registrationDeadline);

}
